package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.ClientzhifuPayBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserZhifupayActivity extends BaseActivity {
    private EditText editText, editText2;
    private Button button2;
    private String token;
    private ImageView icon_back;

    @Override
    protected void initListener() {

        editText2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editText2.getText().length()>0) {
                    button2.setBackgroundResource(R.drawable.shape_btn_finish);
                    button2.setTextColor(Color.parseColor("#FFFFFF"));
                } else {
                    button2.setBackgroundResource(R.drawable.shape_btn_getcode);
                    button2.setTextColor(Color.parseColor("#999999"));
                }
            }
        });
        button2.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                String edit_number = editText.getText().toString();
                String edit_name = editText2.getText().toString();
                getbindingWay(token, EXTRA, "1", edit_number, edit_name);

            }

            @Override
            protected void onFastClick() {

            }
        });
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_user_zhifupay);
        setStatusBarTranslucent();

    }

    @Override
    protected void initView() {
        editText = findViewById(R.id.editText);
        editText2 = findViewById(R.id.editText2);
        button2 = findViewById(R.id.button2);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        icon_back = findViewById(R.id.icon_back);
    }

    private void getbindingWay(String token, String extra, String type, String payNo, String payName) {
        getRequest();
        NetUtils.getInstance().getApiService().getbindingWay(token, extra, type, payNo, payName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                            LogUtils.i("decryptStrzhifupay", decryptStr);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Gson gson = new Gson();
                        ClientzhifuPayBean clientzhifuPayBean = gson.fromJson(decryptStr, ClientzhifuPayBean.class);
                        if (clientzhifuPayBean.getCode() == 0) {
                            ToastUtils.showShort(R.string.mi_bangdingchenggong);
                            finish();
                        } else {
                            ToastUtils.showShort(clientzhifuPayBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}